
<%@page import="transfer.Comment"%>
<%@page import="transfer.OrderState"%>
<%@page import="transfer.Commodity"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@page import="content.OrderContent"%>
<%@page import="controller.SessionAttribute"%>
<%@page import="transfer.User"%>
<%@page import="transfer.Order"%>
<%
    String idOrderString = request.getParameter("order");
    if (idOrderString == null) {
%>
<script> window.location.replace("index.jsp");</script>
<%            } else {
    try {
        Integer idOrder = Integer.valueOf(idOrderString);
        Order order = OrderContent.getOrder(idOrder);
        if (order == null || order.getOrderState().equals(OrderState.ENTERING)) {
%>
<script> window.location.replace("index.jsp");</script>
<%    } else {
    session = request.getSession();
    User user = (User) session.getAttribute(SessionAttribute.USER);    
    if (user != null && order.getCustomer().equals(user) || user == null && order.getCustomer() == null) {
        ArrayList comments = OrderContent.getAllCommentsForOrder(order);
%>

<div class="block">
    <script type="text/javascript" src="js/condition_view.js"></script>
    <h3>Order #<%= order.getIdOrder()%></h3>
    <div class="box">
        <table class="table" rules=all>
            <tr>
                <td>Date</td>
                <td>State</td>
                <td>User name</td>
                <td>Comment</td>
            </tr>
            <%
                for (int i = 0; i < comments.size(); i++) {
                    Comment comment = (Comment) comments.get(i);

            %>
            <tr>
                <td><%= comment.getCommentDate()%></td>
                <td><%= comment.getOrderState().getNameOrderState()%></td>
                <td><%= (comment.getUser() != null) ? comment.getUser().getFirstName() + " " + comment.getUser().getLastName() : ""%></td>
                <td><%= comment.getCommentText()%></td>
            </tr>
            <%
                }
            %>
        </table>
    </div>
    <table id="cart-table" border="0" rules=rows bordercolor="#efefef">
        <tr>
            <td width="100px">Items</td>
            <td></td>
            <td width="60px">Quantity</td>
            <td width="120px"  style="text-align: right;">Price</td>
        </tr>
        <%
            HashMap map = order.getCommodities();
            Set keySet = map.keySet();
            Iterator iterator = keySet.iterator();
            while (iterator.hasNext()) {
                Commodity commodity = (Commodity) iterator.next();
                Integer quantity = (Integer) map.get(commodity);
        %>
        <tr>
            <td><a href="index.jsp?content=view&id=<%= commodity.getIdCommodity()%>"><img src="images/commodity/<%= commodity.getImage()%>"></a></td>
            <td><a href="index.jsp?content=view&id=<%= commodity.getIdCommodity()%>"><%= commodity.getNameCommodity()%></a></td>
            <td><%= quantity %></td>
            <td style="text-align: right;"><b><%= commodity.getPrice() + " $" %></b></td>
        </tr>
        <%
            }
        %>
    </table>
    <div style="width: 854px; text-align: right; font-size: 16px; font-weight: bold; margin: 5px 0px 5px 0px; padding-top: 15px;">
        <span style="margin-right: 70px;">Total:</span><%= order.getCostCommodities() + " $"%>
    </div>
    <div class="table-view">
        <div>Current state:</div><%= order.getOrderState().getNameOrderState()%><br/>
        <% if (order.getCustomer() != null) {%> <div>Customer name:</div> <%= order.getCustomer().getFirstName() + " " +order.getCustomer().getLastName()%><br/> <% }%>
        <div>Location:</div><%= order.getAddress()%><br/>
        <div>Distance:</div><%= order.getDistance() + " km"%><br/>
        <div>Shipment price:</div><%= order.getCostDelivery() + " $"%><br/>
        <div>Total price:</div><%= order.getTotalCost() + " $"%><br/>
        <div>Creation data:</div><%= order.getCreationDate()%><br/>
    </div>
    <br/>
    <%
        if (order.getIsSubscribed().booleanValue()) {                
    %>
        <script>            
            isSubscribed = true;
            email = '<%= order.getEmail() %>';            
        </script>
    <%
        }
    %>
    <%
        Boolean isSigned = (Boolean) session.getAttribute(SessionAttribute.SIGN_IN);        
        if (isSigned == null || !isSigned.booleanValue()){
    %>        
        <script>
            console.log(<%= order.getIsSubscribed().booleanValue() %>);
            isSigned = false;
        </script>
        <div>
            <b>E-mail: </b><input id="email" type="email" required="">             
        </div>
    <%            
        }
    %>
    <h id="check-send-email_text"><input type="checkbox" id="check-send-email" onclick="sendMailClicked(<%= order.getIdOrder()%>)"> Send information about order to e-mail.<br/></h>
</div>

<%  } else {
%>
<script> window.location.replace("index.jsp");</script>
<%    } 
}
} catch (NumberFormatException ex) {
    ex.printStackTrace();
%>
<script> window.location.replace("index.jsp");</script>
<%
        }
    }
%>



